package com.mcy.springbatch.controller;

import com.mcy.springbatch.service.BatchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.support.PropertiesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Properties;

/**
 * @author : Create By mcy
 * @date : 2019/8/26 11:15:11
 */
@Slf4j
@RestController
@RequestMapping("batch")
public class BatchController {

    @Autowired
    private BatchService batchService;

    @GetMapping("startJob")
    public String startJob(String jobName,String date,String time){
        if(StringUtils.isBlank(jobName)){
            return "jobName is null";
        }

        JobParameters jobParameters =  new JobParametersBuilder()
                .addString("sys_date",date)
                .addString("sys_time",time)
                .toJobParameters();

        JobExecution jobExecution = null;
        try {
            jobExecution = batchService.launch(jobName, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(jobExecution != null){
            return "start a job success!";
        }
        return "start a job failed!";
    }

}
